//
//  WZProcedureLogsController.m
//  ETong
//
//  Created by wanzhao on 15/12/11.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZProcedureLogsController.h"
#import "CFolderMgr.h"

#import "WZProcedureLogCell.h"
#import "UIBarButtonItem+category.h"
#import "MBProgressHUD+WZ.h"
#import "NetWorkShare.h"

@interface WZProcedureLogsController ()<UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>

@property (nonatomic, strong) CAccountModel *curAccount;
@property (nonatomic, strong) NSMutableArray *logArray;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIButton *cover;

@end

@implementation WZProcedureLogsController

- (void)dealloc
{
    NSLog(@"WZProcedureLogsController dealloc");
}


- (NSMutableArray *)logArray
{
    if (_logArray == nil) {
        _logArray = [NSMutableArray array];
    }
    return _logArray;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupLogView];
    
    [self loadLogsData];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!IsIOS7) {
        self.navigationController.navigationBar.tintColor = RGBCOLORWITH16COLOR(0xa694d4);
    } else {
        self.navigationController.navigationBar.barTintColor = RGBCOLORWITH16COLOR(0xa694d4);
    }
    
}

- (void)setupLogView {
    
    self.navigationItem.title = @"流程日志";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"shuaxin" title:nil target:self sel:@selector(shuaXinShuJu) titleFont:Font10 titleColor:WhiteColor];
//    self.curAccount = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    
    self.view.backgroundColor = WhiteColor;
    
    UITableView *rightView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    rightView.frame = CGRectMake(0, 0, self.view.width, HARDWARE_SCREEN_HEIGHT - 64.0f);
    rightView.backgroundColor = ClearColor;
    rightView.delegate = self;
    rightView.dataSource = self;
    rightView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:rightView];
    self.tableView = rightView;
}

- (void)shuaXinShuJu {
    [self loadLogsData];
}


- (void)loadLogsData {
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    [NetWorkShare liuChengRiZhiWithcaoGaoXiangWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID processInstanceId:self.processInstId successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                NSArray * array = [WZProcedureLog objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                [self.logArray removeAllObjects];
                [self.logArray addObjectsFromArray:array];
                WZProcedureLog *begin = nil;
                WZProcedureLog *end = nil;
                for (WZProcedureLog *log in self.logArray) {
                    if ([@"开始" isEqualToString:log.taskName]) {
                        log.isShortCell = YES;
                        begin = log;
                    } else {
                        log.isShortCell = NO;
                        if ([@"结束" isEqualToString:log.taskName]) {
                            end = log;
                        }
                    }
                }
                if (begin) {
                    [self.logArray removeObject:begin];
                    [self.logArray insertObject:begin atIndex:0];
                }
                if (end) {
                    [self.logArray removeObject:end];
                    [self.logArray addObject:end];
                }
                if (self.tableView.window) [self.tableView reloadData];
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }

        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

#pragma mark tableView相关方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.logArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZProcedureLog *log = self.logArray[indexPath.row];
    if (log.isShortCell) {
        return OppositeHeightRate(60);
    } else {
        return OppositeHeightRate(92);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZProcedureLogCell *cell = [WZProcedureLogCell procedureLogCellWithTableView:tableView];
   
    WZProcedureLog *log = self.logArray[indexPath.row];
    cell.procedureLog = log;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZProcedureLog *log = self.logArray[indexPath.row];
    [self showLogDetailWithLog:log];
}

- (void)showLogDetailWithLog:(WZProcedureLog *)log
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIButton *cover = [UIButton buttonWithType:UIButtonTypeCustom];
    cover.frame = [UIScreen mainScreen].bounds;
    cover.backgroundColor = RGBACOLORWithAlpha(171, 171, 171, 0.6f);
    [window addSubview:cover];
    self.cover = cover;

    UIView *neirong = [[UIView alloc] init];
    neirong.backgroundColor = WhiteColor;
    neirong.frame = CGRectMake(OppositeWidthRate(40), OppositeHeightRate(134), HARDWARE_SCREEN_WIDTH - OppositeWidthRate(80), HARDWARE_SCREEN_HEIGHT - OppositeHeightRate(268));
    [cover addSubview:neirong];
    
    UILabel *tit = [[UILabel alloc] init];
    tit.font = Font14;
    tit.textColor = RGBCOLORWITH16COLOR(0x333333);
    tit.frame = CGRectMake(OppositeWidthRate(15), OppositeHeightRate(34), neirong.width, Font14.lineHeight);
    tit.text = @"执行意见：";
    [neirong addSubview:tit];
    
    UIButton *guanbi = [UIButton buttonWithType:UIButtonTypeCustom];
    [guanbi addTarget:self action:@selector(guanbi) forControlEvents:UIControlEventTouchUpInside];
    [guanbi setImage:[UIImage imageNamed:@"close.jpg"] forState:UIControlStateNormal];
    guanbi.adjustsImageWhenHighlighted = NO;
    guanbi.frame = CGRectMake(neirong.width - OppositeWidthRate(15) - OppositeWidthRate(34), 0, OppositeWidthRate(34), OppositeWidthRate(34));
    [neirong addSubview:guanbi];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBCOLORWITH16COLOR(0xe5e5e5);
    line.frame = CGRectMake(OppositeWidthRate(15), CGRectGetMaxY(tit.frame) + OppositeHeightRate(10), neirong.width - OppositeWidthRate(30), 0.5f);
    [neirong addSubview:line];
    
    UITextView *textView = [[UITextView alloc] init];
    textView.frame = CGRectMake(OppositeWidthRate(15), CGRectGetMaxY(line.frame), neirong.width - OppositeWidthRate(30), neirong.height - CGRectGetMaxY(line.frame));
    textView.font = Font12;
    textView.delegate = self;
    textView.textColor = RGBCOLORWITH16COLOR(0x666666);
    textView.text = [NSString stringWithFormat:@"       %@",log.comment];
    [neirong addSubview:textView];
    
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}
- (void)guanbi
{
    [self.cover removeFromSuperview];
    self.cover = nil;
}
@end
